package com.meizi.utils;

import com.meizi.enums.PatientMemberGenderEnum;
import org.junit.platform.commons.util.StringUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CardUtil {
    /**
     * 根据身份证的号码算出当前身份证持有者的性别和年龄 18位身份证
     *
     * @return
     * @throws Exception
     */
    public static Map<String, Object> getCarInfo(String CardCode) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();

        if(StringUtils.isBlank(CardCode)){
            return null ;
        }

        String year = "" ;
        String yue = "" ;
        String day = "" ;
        String sex = "" ;
        Integer gender = 0;

        if(CardCode.length() == 15){
            year = "19" + CardCode.substring(6, 8);
            yue = CardCode.substring(8, 10);
            day = CardCode.substring(10, 12);
            sex = CardCode.substring(14, 15);
        }

        if(CardCode.length() == 18){
            year = CardCode.substring(6).substring(0, 4);
            yue = CardCode.substring(10).substring(0, 2);
            day = CardCode.substring(12).substring(0,2);
            sex = CardCode.substring(16).substring(0, 1) ;
        }

        // 判断性别
        if (Integer.parseInt(sex) % 2 == 0) {
            gender = PatientMemberGenderEnum.GIRL.getValue();
        } else {
            gender = PatientMemberGenderEnum.BOY.getValue();
        }

        // 得到当前的系统时间,年月日
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String fyear = format.format(date).substring(0, 4);
        String fyue = format.format(date).substring(5, 7);
        String fday = format.format(date).substring(8,10);
        int age = 0;

        // 当前月份大于用户出身的月份表示已过生
        if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
            age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1;
        } else {// 当前用户还没过生
            age = Integer.parseInt(fyear) - Integer.parseInt(year);
        }
        map.put("gender", gender);
        map.put("age", age);

        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        map.put("birthday", sdf.parse(year + "-" + yue + "-" + day));

        return map;
    }

    public static Integer getAge(Date birthday){ if(birthday == null){
            return 0 ;
        }

        // 出生年月日
        String year = String.format("%tY", birthday);
        String yue = String .format("%tm", birthday);
        String day = String .format("%td", birthday);


        // 得到当前的系统时间,年月日
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String fyear = format.format(date).substring(0, 4);
        String fyue = format.format(date).substring(5, 7);
        String fday = format.format(date).substring(8,10);
        int age = 0;

        // 当前月份大于用户出身的月份表示已过生
        if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) {
            if(Integer.parseInt(yue) == Integer.parseInt(fyue)){
                if (Integer.parseInt(day) >= Integer.parseInt(fday)) {
                    age = Integer.parseInt(fyear) - Integer.parseInt(year) -1 ;
                }else{
                    age = Integer.parseInt(fyear) - Integer.parseInt(year) ;
                }
            }else{
                age = Integer.parseInt(fyear) - Integer.parseInt(year) ;
            }
        } else {// 当前用户还没过生
            age = Integer.parseInt(fyear) - Integer.parseInt(year) - 1;
        }
        return age ;
    }

    public static void main(String[] args) {

    }
}
